const router = require('koa-router')()
const CreateSequelize = require('./sequelize')
const { DataTypes, Op } = require('sequelize');
const Sequelize = require('sequelize')
let sequelize = new CreateSequelize()
sequelize.init_sequeleze()
const infos = sequelize.db.define('infos', {
    info_id: {
        type: DataTypes.UUID,
        defaultValue: Sequelize.UUIDV4,
        comment: '用户唯一标识'
    },
    account: {
        type: DataTypes.STRING,
        comment: '帐号'
    },
    pass: {
        type: DataTypes.STRING,
        comment: '密码'
    },
    name: {
        type: DataTypes.STRING,
        comment: '名称'
    },
    headPortrait: {
        type: DataTypes.STRING,
        comment: '头像'
    },
    site: {
        type: DataTypes.STRING,
        comment: '地址'
    },
    QRCode: {
        type: DataTypes.STRING,
        comment: '二维码'
    },
    phoneNum: {
        type: DataTypes.STRING,
        comment: '手机号'
    },
    friendCircle: {
        type: DataTypes.STRING(10000),
        comment: '朋友圈'
    },
    sex: {
        type: DataTypes.STRING,
        comment: '性别'
    },
    signature: {
        type: DataTypes.STRING,
        comment: '个性签名'
    },
    collect: {
        type: DataTypes.STRING(1000),
        comment: '收藏'
    },
    qqaccount: {
        type: DataTypes.STRING,
        comment: 'qq号'
    },
    mailbox: {
        type: DataTypes.STRING,
        comment: '邮箱号'
    }
}, {
    timestamps: false
})
router.prefix('/my')
router.get('/', function (ctx, next) {
    ctx.body = 'this is a users response!'
})


//手机号登录
router.post('/phoneNum', async function (ctx, next) {
    // let newData = await sequelize.select("select * from cardata")
    let { telNum, pasw } = ctx.request.body
    let data = await infos.findOne({
        where: {
            phoneNum: telNum
        }
    })
    if (data) {
        if (data.pass === pasw) {
            ctx.body = {
                code: 0,
                message: '登录成功',
                // Userid: data.id
                data
            }
        } else {
            ctx.body = {
                code: 1,
                message: '密码错误',
            }
        }
    } else {
        ctx.body = {
            code: 2,
            message: '该手机号没有注册或手机号错误'
        }
    }
})

//邮箱微信号登录 (其他帐号)
router.post('/Otherways', async function (ctx, next) {
    let { account, pass } = ctx.request.body
    let data = await infos.findOne({
        where: {
            [Op.or]: [
                { account: account },
                { qqaccount: account }
            ]
        }
    })
    if (data) {
        if (data.pass === pass) {
            ctx.body = {
                code: 0,
                message: '登录成功',
                // Userid: data.id
                data
            }
        } else {
            ctx.body = {
                code: 1,
                message: '密码错误',
            }
        }
    } else {
        ctx.body = {
            code: 2,
            message: '该账号不存在'
        }
    }
})

//验证码登录
router.post('/verification', async function (ctx, next) {
    let { telNum } = ctx.request.body
    const vercode = await Math.random().toString().slice(2, 8);
    let data = await infos.findOne({
        where: {
            phoneNum: telNum
        }
    })
    console.log(data);
    if (data) {
        // 向手机号发送验证码
        // await axios({
        //   url: "https://106.ihuyi.com/webservice/sms.php?method=Submit",
        //   method: "get",
        //   params: {
        //     account: 'C46356220',
        //     password: '169a385cd3ec5f8bac02d409d3595db4',
        //     mobile: telNum,
        //     content: `您的验证码是：${vercode}。请不要把验证码泄露给其他人。`
        //   }
        // })
        ctx.body = {
            code: 1,
            message: '短信已发送',
            vercode,
            Userid: data.id
        }
    } else {
        ctx.body = {
            code: 2,
            message: '该手机还没有被注册',
            verstate: false
        }
    }

})

//注册验证码
router.post('/rigcode', async (ctx, next) => {
    let { tel } = ctx.request.body
    const vercode = await Math.random().toString().slice(2, 8);
    let data = await infos.findOne({
        where: {
            phoneNum: tel
        }
    })
    if (data) {
        ctx.body = {
            code: 1,
            message: '该手机号已被注册',
            verstate: false
        }
    } else {
        // 向手机号发送验证码
        // await axios({
        //   url: "https://106.ihuyi.com/webservice/sms.php?method=Submit",
        //   method: "get",
        //   params: {
        //     account: 'C46356220',
        //     password: '169a385cd3ec5f8bac02d409d3595db4',
        //     mobile: tel,
        //     content: `您的验证码是：${vercode}。请不要把验证码泄露给其他人。`
        //   }
        // })
        ctx.body = {
            code: 0,
            message: '短信已发送',
            vercode,
            Userid: data
        }
    }
})

// 注册微信
router.post('/register', async (ctx, netx) => {
    let { name, tel, pasw } = ctx.request.body
    let data = await infos.findOne({
        where: {
            phoneNum: tel
        }
    })
    if(data){
        ctx.body = {
            code:0,
            message:"该手机号已被注册"
        }
    }else{
        await infos.create({
            name,
            phoneNum:tel,
            pass:pasw
        })
        ctx.body = {
            code:1,
            message:"注册成功",
            Userid: data
        }
    }
})
module.exports = router


